<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 孤焰
  Date: 2021/6/12
  Time: 14:04
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>收货地址编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">收货地址编辑</div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input value="${shippingInfo.user.username}" readonly="readonly" type="text" name="username" lay-verify="required" autocomplete="off" placeholder="请输入用户名"
                               class="layui-input layui-disabled">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">收货人名称</label>
                    <div class="layui-input-block">
                        <input value="${shippingInfo.receiverName}" type="text" name="receiverName" lay-verify="required" autocomplete="off" placeholder="请输入收货人姓名"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">收货人电话</label>
                    <div class="layui-input-block">
                        <input value="${shippingInfo.receiverPhone}" type="text" name="receiverPhone" lay-verify="phone" autocomplete="off" placeholder="请输入收货人电话"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item" id="area-picker">
                    <label class="layui-form-label">收货地址</label>
                    <div class="layui-input-block">
                        <div class="layui-inline">
                            <select name="receiverProvince" data-value="${shippingInfo.receiverProvince}" class="province-selector" lay-filter="province">
                                <option value="">请选择省</option>
                            </select>
                        </div>
                        <div class="layui-inline">
                            <select name="receiverCity" data-value="${shippingInfo.receiverCity}" class="city-selector" lay-filter="city">
                                <option value="">请选择市</option>
                            </select>
                        </div>
                        <div class="layui-inline">
                            <select name="receiverDistrict" data-value="${shippingInfo.receiverDistrict}" class="county-selector" lay-filter="county">
                                <option value="">请选择县/区</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">详细地址</label>
                    <div class="layui-input-block">
                        <input value="${shippingInfo.receiverAddress}" type="text" name="receiverAddress" lay-verify="required" autocomplete="off" placeholder="请输入详细地址"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">邮政编码</label>
                    <div class="layui-input-block">
                        <input value="${shippingInfo.receiverZip}" type="text" name="receiverZip" lay-verify="receiverZip" autocomplete="off" placeholder="请输入邮政编码"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button class="layui-btn" lay-submit="" lay-filter="shipping-submit">立即提交</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


<script src="${pageContext.request.contextPath}/layuiadmin/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/static/js/axios.min.js"></script>
<script>
    layui.config({
        base: '${pageContext.request.contextPath}/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate', 'layarea'], function () {
        var layer = layui.layer;
        var form = layui.form;
        var layarea = layui.layarea;

        layarea.render({
            elem: '#area-picker'
        });

        // 自定义表单验证
        form.verify({
            receiverZip: [/^[0-8][0-7]\d{4}$/, '邮政编码格式不合法']
        });

        /* 监听提交 */
        form.on('submit(shipping-submit)', function (data) {
            var shipping = data.field;
            shipping.id = ${shippingInfo.id};
            shipping.userId = ${shippingInfo.userId};
            delete shipping.username;

            axios.post('${pageContext.request.contextPath}/shipping/updateShippingById', shipping).then(function (res) {
                if (res.data.code == 200) {
                    layer.msg("数据更新成功", {icon: 6});
                    setTimeout(function () {
                        parent.location.reload();// 刷新父窗口
                        window.parent.layer.closeAll();//关闭弹窗
                    }, 800);
                } else {
                    layer.msg(res.data.message, {icon: 5});
                }
            });
            return false;
        });
    });
</script>
</body>
</html>
